package com.yoomiito.app.ui.my.angle

import android.graphics.Bitmap
import android.text.TextUtils
import android.util.Log
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.my.AgreementInfo
import com.yoomiito.app.net.UploadFileManager
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.utils.YMToast

class SignPresenter(appConfig: AppConfig) :BaseActPresenter<SignActivity>(appConfig){

    fun uploadPhotos(path: Bitmap,agreementUrl:String) {

        if (path!=null) {
            v.showLoading()
            UploadFileManager.uploadFile(path, "0", object : UploadFileManager.UploadFileCallBack {
                override fun uploadSucc(filename: String, key: String) {
                    Log.e("lin", "imageUrl-->$filename")
                    commitSign(filename,agreementUrl)
                }
                override fun uploadFail(code: Int, key: String) {
                    v.dismissLoading()
                    YMToast.showText("上传失败")
                }
            })
        }
    }

    fun commitSign(url:String,agreementUrl:String){
        YmApi.getYmService()
                .commitAngleSign(url,agreementUrl)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<AgreementInfo>(handleException()){
                    override fun onSuccess(t: AgreementInfo?) {
                        v.dismissLoading()
                        v.showTip(t?.url)
                    }

                    override fun onFail(error: ApiException?) {
                        v.dismissLoading()
                    }
                })
    }
}